package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.contants.SystemConstants;
import com.sangeng.entity.ResponseResult;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.mapper.Exception.SystemException;
import com.sangeng.mapper.TagDao;
import com.sangeng.entity.Tag;
import com.sangeng.service.TagService;
import com.sangeng.util.BeanCopyUtils;
import com.sangeng.vo.PageVo;
import com.sangeng.vo.TagVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2022-08-27 09:57:29
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagDao, Tag> implements TagService {
     @Resource
    TagDao tagDao;
    @Override
    public PageVo SelectTagPageVo(Integer pageNum, Integer pageSize, TagVo tagVo) {
        LambdaQueryWrapper<Tag> tagLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //模糊查询
        tagLambdaQueryWrapper.like(StringUtils.hasText(tagVo.getName()),Tag::getName,tagVo.getName());
        //tagLambdaQueryWrapper.eq(StringUtils.hasText(tagVo.getName()),Tag::getName,tagVo.getName());
      //  tagLambdaQueryWrapper.eq(StringUtils.hasText(tagVo.getRemark()),Tag::getRemark,tagVo.getRemark());
         //分页处理
        tagLambdaQueryWrapper.like(StringUtils.hasText(tagVo.getRemark()),Tag::getRemark,tagVo.getRemark());
        Page<Tag> page = new Page<>(pageNum, pageSize);
        Page<Tag> tagPage = page(page, tagLambdaQueryWrapper);
        List<Tag> records = tagPage.getRecords();
        long total = tagPage.getTotal();
        PageVo pageVo = new PageVo(records, total);
        return pageVo;
    }

    @Override
    public ResponseResult addTag(TagVo tagVo) {

        if(!StringUtils.hasText(tagVo.getName())){
          throw   new SystemException(AppHttpCodeEnum.TAG_NOT_NULL);

        }else if(!StringUtils.hasText(tagVo.getRemark())){
            throw   new SystemException(AppHttpCodeEnum.REMARK_NOT_NULL);
        }
        Tag tag = BeanCopyUtils.copyBean(tagVo, Tag.class);
        boolean save = save(tag);
        if(save){
            return ResponseResult.okResult();
        }
        else{
            return ResponseResult.errorResult(AppHttpCodeEnum.TAG_INSERT_ERROR);
        }

    }

    @Override
    public ResponseResult DeleteTag(List<Long> idList) {

        for (Long id: idList) {
            LambdaUpdateWrapper<Tag> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            LambdaUpdateWrapper<Tag> set = lambdaUpdateWrapper.eq(Tag::getId, id).set(Tag::getDelFlag, 1);
            int update = tagDao.update(null, set);
            if(update==0){
                return ResponseResult.errorResult(AppHttpCodeEnum.TAG_DELETE_ERROR);
            }
        }
        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult getTagInfo(Long id) {
        Tag tag = tagDao.selectById(id);
        if(Objects.isNull(tag)){
            throw new SystemException(AppHttpCodeEnum.TAG_GETINFO_ERROR);

        }
        HashMap<String, Object> tagMap = new HashMap<>();
        tagMap.put("id",tag.getId());
        tagMap.put("name",tag.getName());
        tagMap.put("remark",tag.getRemark());


        return ResponseResult.okResult(tagMap);
    }

    @Override
    public ResponseResult updateTagInfo(TagVo tagVo) {

        LambdaUpdateWrapper<Tag> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        LambdaUpdateWrapper<Tag> set = lambdaUpdateWrapper.eq(Tag::getId, tagVo.getId()).set(Tag::getName, tagVo.getName()).set(Tag::getRemark,tagVo.getRemark());
        int update = tagDao.update(null, set);
        if(update>0){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.TAG_UPDATE_ERROR);
        }

    }
}

